﻿CREATE TRIGGER [dbo].[trPlayersUpdate]
ON [dbo].[Players]
FOR UPDATE 
AS    

BEGIN
    SET NOCOUNT ON	

	INSERT INTO [PlayerHistories]
           ([PlayerId]
           ,[Login]
           ,[Picture]
           ,[LodzOfficeRoomId]
           ,[AccountPoints]           
           ,[InsertUser]
           ,[InsertDate]
           ,[UpdateUser]
           ,[UpdateDate]
           ,[HistoryInsertUser]
           ,[HIstoryInsertDate]
           ,[Deleted])
     SELECT 
		   i.[Id]
		  ,i.[Login]
		  ,i.[Picture]
		  ,i.[LodzOfficeRoomId]
		  ,i.[AccountPoints]		 
		  ,i.[InsertUser]
		  ,i.[InsertDate]
		  ,i.[UpdateUser]
		  ,i.[UpdateDate]		  
		  ,isnull(i.[UpdateUser], 'trigger trPlayersUpdate')
		  ,isnull(i.[UpdateDate], getdate())
		  ,i.[Deleted]
	FROM [Players] p
		inner join deleted d on d.Id = p.Id
		inner join inserted i on i.Id = p.Id
	WHERE d.[AccountPoints] <> i.[AccountPoints]

	
END